using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace XZ.Logistics.WebShop
{
    public class OrderStatus : WebShopBase
    {

        private int _id = 0;
        public new int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _title = "";
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public OrderStatus(int id, DateTime addedDate, string addedBy, string title)
        {
            this.Id = id;
            this.AddedDate = addedDate;
            this.AddedBy = addedBy;
            this.Title = title;
        }

        public bool Delete()
        {
            bool success = OrderStatus.DeleteOrderStatus(this.Id);
            if (success)
                this.Id = 0;
            return success;
        }

        public bool Update()
        {
            return OrderStatus.UpdateOrderStatus(this.Id, this.Title);
        }

        /***********************************
        * Static methods
        ************************************/

        /// <summary>
        /// Returns a collection with all the order statuses
        /// </summary>
        public static List<OrderStatus> GetOrderStatuses()
        {
            List<OrderStatus> orderStatuses = null;
            string key = "WebShop_OrderStatuses";

            if (WebShopBase.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                orderStatuses = (List<OrderStatus>)BizObject.Cache[key];
            }
            else
            {
                orderStatuses = WebShop.GetOrderStatuses();
                WebShopBase.CacheData(key, orderStatuses);
            }
            return orderStatuses;
        }

        /// <summary>
        /// Returns a OrderStatus object with the specified ID
        /// </summary>
        public static OrderStatus GetOrderStatusById(int orderStatusId)
        {
            OrderStatus orderStatus = null;
            string key = "WebShop_OrderStatus_" + orderStatusId.ToString();

            if (WebShopBase.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                orderStatus = (OrderStatus)BizObject.Cache[key];
            }
            else
            {
                orderStatus = WebShop.GetOrderStatusById(orderStatusId);
                WebShopBase.CacheData(key, orderStatus);
            }
            return orderStatus;
        }

        /// <summary>
        /// Updates an existing order status
        /// </summary>
        public static bool UpdateOrderStatus(int id, string title)
        {
            OrderStatus record = new OrderStatus(id, DateTime.Now, "", title);
            bool ret = WebShop.UpdateOrderStatus(record);
            BizObject.PurgeCacheItems("WebShop_OrderStatus");
            return ret;
        }

        /// <summary>
        /// Deletes an existing order status
        /// </summary>
        public static bool DeleteOrderStatus(int id)
        {
            bool ret = WebShop.DeleteOrderStatus(id);
            //new RecordDeletedEvent("order status", id, null).Raise();
            BizObject.PurgeCacheItems("WebShop_OrderStatus");
            return ret;
        }

        /// <summary>
        /// Creates a new order status
        /// </summary>
        public static int InsertOrderStatus(string title)
        {
            OrderStatus record = new OrderStatus(0, DateTime.Now,
               BizObject.CurrentUserName, title);
            int ret = WebShop.InsertOrderStatus(record);
            BizObject.PurgeCacheItems("WebShop_OrderStatus");
            return ret;
        }
    }
}